#include "usart_buffer.h"

u8 buffer[USART_BUFFER_SIZE];
u8 head = 0;
u8 tail = 0;

u8 bufferEmpty(USART_TypeDef* USARTx)
{
    return head;
}


u8 writeBuffer(USART_TypeDef* USARTx, u8 c)
{
    buffer[head++] = c;
    if(head == tail)
    {
        tail++;
        if(tail >= USART_BUFFER_SIZE)
            tail = 0;
    }
    if(head >= USART_BUFFER_SIZE)
        head = 0;
    
    return head;
}


u8 readBuffer( USART_TypeDef* USARTx )
{
    u8 c = buffer[tail++];
    if(tail >= USART_BUFFER_SIZE)
        tail = 0;
}


u8 flushBuffer( USART_TypeDef* USARTx )
{
    head = tail = 0;
}


tHandler USART1_IRQHandler( void )
{
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    USART_ClearITPendingBit(USART1, USART_IT_TXE);
}

